/**
 * Copyright &copy; 2012-2014 <a href="http://www.iframework.cn">Mas</a> All rights reserved.
 */
package com.iframework.mas.modules.accident.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iframework.mas.common.persistence.Page;
import com.iframework.mas.common.service.CrudService;
import com.iframework.mas.common.utils.StringUtils;
import com.iframework.mas.modules.accident.entity.Accident;
import com.iframework.mas.modules.accident.dao.AccidentDao;
import com.iframework.mas.modules.accident.entity.Accidentcar;
import com.iframework.mas.modules.accident.dao.AccidentcarDao;
import com.iframework.mas.modules.accident.entity.Accidenter;
import com.iframework.mas.modules.accident.dao.AccidenterDao;
import com.iframework.mas.modules.accident.entity.Accidentlocation;
import com.iframework.mas.modules.accident.dao.AccidentlocationDao;
import com.iframework.mas.modules.accident.entity.Accidentpic;
import com.iframework.mas.modules.accident.dao.AccidentpicDao;
import com.iframework.mas.modules.accident.entity.Accidentresult;
import com.iframework.mas.modules.accident.dao.AccidentresultDao;

/**
 * 事故列表Service
 * @author 余艾龙
 * @version 2016-01-30
 */
@Service
@Transactional(readOnly = true)
public class AccidentService extends CrudService<AccidentDao, Accident> {

	@Autowired
	private AccidentcarDao accidentcarDao;
	@Autowired
	private AccidenterDao accidenterDao;
	@Autowired
	private AccidentlocationDao accidentlocationDao;
	@Autowired
	private AccidentpicDao accidentpicDao;
	@Autowired
	private AccidentresultDao accidentresultDao;
	
	public Accident get(String id) {
		Accident accident = super.get(id);
		accident.setAccidentcarList(accidentcarDao.findList(new Accidentcar(accident)));
		accident.setAccidenterList(accidenterDao.findList(new Accidenter(accident)));
		accident.setAccidentlocation(accidentlocationDao.get(new Accidentlocation(accident)));
		accident.setAccidentpicList(accidentpicDao.findList(new Accidentpic(accident)));
		accident.setAccidentresult(accidentresultDao.get(new Accidentresult(accident)));
		return accident;
	}
	
	public List<Accident> findList(Accident accident) {
		return super.findList(accident);
	}
	
	public Page<Accident> findPage(Page<Accident> page, Accident accident) {
		return super.findPage(page, accident);
	}
	
	@Transactional(readOnly = false)
	public void save(Accident accident) {
		super.save(accident);
		for (Accidentcar accidentcar : accident.getAccidentcarList()){
			if (accidentcar.getId() == null){
				continue;
			}
			if (Accidentcar.DEL_FLAG_NORMAL.equals(accidentcar.getDelFlag())){
				if (StringUtils.isBlank(accidentcar.getId())){
					accidentcar.setAccident(accident);
					accidentcar.preInsert();
					accidentcarDao.insert(accidentcar);
				}else{
					accidentcar.preUpdate();
					accidentcarDao.update(accidentcar);
				}
			}else{
				accidentcarDao.delete(accidentcar);
			}
		}
		for (Accidenter accidenter : accident.getAccidenterList()){
			if (accidenter.getId() == null){
				continue;
			}
			if (Accidenter.DEL_FLAG_NORMAL.equals(accidenter.getDelFlag())){
				if (StringUtils.isBlank(accidenter.getId())){
					accidenter.setAccident(accident);
					accidenter.preInsert();
					accidenterDao.insert(accidenter);
				}else{
					accidenter.preUpdate();
					accidenterDao.update(accidenter);
				}
			}else{
				accidenterDao.delete(accidenter);
			}
		}
		//for (Accidentlocation accidentlocation : accident.getAccidentlocation()){
		Accidentlocation accidentlocation= accident.getAccidentlocation();
//			if (accidentlocation.getId() == null){
//				continue;
//			}
			if (Accidentlocation.DEL_FLAG_NORMAL.equals(accidentlocation.getDelFlag())){
				if (StringUtils.isBlank(accidentlocation.getId())){
					accidentlocation.setAccident(accident);
					accidentlocation.preInsert();
					accidentlocationDao.insert(accidentlocation);
				}else{
					accidentlocation.preUpdate();
					accidentlocationDao.update(accidentlocation);
				}
			}else{
				accidentlocationDao.delete(accidentlocation);
			}
		//}
		for (Accidentpic accidentpic : accident.getAccidentpicList()){
			if (accidentpic.getId() == null){
				continue;
			}
			if (Accidentpic.DEL_FLAG_NORMAL.equals(accidentpic.getDelFlag())){
				if (StringUtils.isBlank(accidentpic.getId())){
					accidentpic.setAccident(accident);
					accidentpic.preInsert();
					accidentpicDao.insert(accidentpic);
				}else{
					accidentpic.preUpdate();
					accidentpicDao.update(accidentpic);
				}
			}else{
				accidentpicDao.delete(accidentpic);
			}
		}
		//for (Accidentresult accidentresult : accident.getAccidentresult()){
		Accidentresult accidentresult =accident.getAccidentresult();
//			if (accidentresult.getId() == null){
//				continue;
//			}
			if (Accidentresult.DEL_FLAG_NORMAL.equals(accidentresult.getDelFlag())){
				if (StringUtils.isBlank(accidentresult.getId())){
					accidentresult.setAccident(accident);
					accidentresult.preInsert();
					accidentresultDao.insert(accidentresult);
				}else{
					accidentresult.preUpdate();
					accidentresultDao.update(accidentresult);
				}
			}else{
				accidentresultDao.delete(accidentresult);
			}
		//}
	}
	
	@Transactional(readOnly = false)
	public void delete(Accident accident) {
		super.delete(accident);
		accidentcarDao.delete(new Accidentcar(accident));
		accidenterDao.delete(new Accidenter(accident));
		accidentlocationDao.delete(new Accidentlocation(accident));
		accidentpicDao.delete(new Accidentpic(accident));
		accidentresultDao.delete(new Accidentresult(accident));
	}
	
}